package com.liangtian.mdm.material.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.dm.common.util.RequestUtils;
import com.liangtian.mdm.material.model.WlwlGd;
import com.liangtian.mdm.material.service.WlwlGdService;
import com.liangtian.mdm.system.log.service.SysLogService;
import com.liangtian.mdm.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wlwlGd")
public class WlwlGdController extends BaseController {

    @Autowired
    private WlwlGdService wlwlGdService;

    // 日志服务接口对象
    @Autowired
    SysLogService sysLogServiceImpl;

    // Session中保存的当前登录用户的信息
    //@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    /**
     * 根据条件查询物料归档数据
     *
     * @param request
     * @param wlwl
     * @return
     * @author jyh
     * @date 2016-09-20
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/queryWlwlGd", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryWlwlGd(HttpServletRequest request, WlwlGd wlwlGd) {
        Map<String, Object> paramMap = RequestUtils.paramToMap(request, WlwlGd.class, false);
        String lbbms = (String) paramMap.get("lbbms");
        if (!StringUtils.isEmpty(lbbms))
            paramMap.put("lbbmArray", StringUtil.splitAsList(lbbms,","));
        String qcs = (String) paramMap.get("qcs");
        if (!StringUtils.isEmpty(qcs))
            paramMap.put("qcArray", spaceToList(qcs));
        PageInfo pageInfo = wlwlGdService.queryWlwlGd(paramMap);
        List<WlwlGd> lst = pageInfo.getList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", lst);// JSONArray
        jsonObject.put("total", pageInfo.getTotal());//
        return jsonObject;
    }

    public static List<String> spaceToList(String array) {
        ArrayList<String> list = new ArrayList<String>();
        if (array != null) {
            for (String string : array.split(" ")) {
                list.add(string);
            }
        }
        return list;
    }
}
